﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OrderSystem;

namespace WebAPI
{
    [Route("api/test")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        OrderService service = new OrderService();
        [Route("add")]
        [HttpGet]
        public string add(string param)
        {
            try
            {
                string[] paras = param.Split(',');
                if (service.addOrder(new Order(new OrderDetails(new Good(paras[0]),new Customer(paras[1]), double.Parse(paras[2])))))
                {
                    return "添加了" + service.getOrder(new OrderDetails(new Good(paras[0]), new Customer(paras[1]), double.Parse(paras[2]))).First();
                }
            }
            catch (Exception)
            {

                return "添加失败";
            }
            return "添加失败";
        }

        [Route("del")]
        [HttpGet]
        public string delOrder(string param)
        {
            try
            {
                string[] paras = param.Split(',');
                if (service.deleteOrder(new Order(new OrderDetails(new Good(paras[0]), new Customer(paras[1]), double.Parse(paras[2])))))
                {
                    return "删除了" + new OrderDetails(new Good(paras[0]), new Customer(paras[1]), double.Parse(paras[2]));
                }
            }
            catch (Exception)
            {

                return "删除失败";
            }
            return "删除失败";
        }

        [Route("modify")]
        [HttpGet]
        public string modify(string param)
        {
            try
            {
                string[] paras = param.Split(',');
                if (service.modifyOrder(int.Parse(paras[0]),new OrderDetails(new Good(paras[1]), new Customer(paras[2]), double.Parse(paras[3]))))
                {
                    return "修改了" + service.getOrder(new OrderDetails(new Good(paras[0]), new Customer(paras[1]), double.Parse(paras[2]))).First();
                }
            }
            catch (Exception)
            {

                return "修改失败";
            }
            return "修改失败";
        }

        [Route("search")]
        [HttpGet]
        public string search(string param)
        {
            try
            {
                string[] paras = param.Split(',');
                string result = "";
                foreach (Order i in service.getOrder(new OrderDetails(new Good(paras[0]), new Customer(paras[1]), double.Parse(paras[2])))){
                    result = result + i + "\n";
                }

                return result;
                
            }
            catch (Exception)
            {

                return "查询失败";
            }
            return "查询失败";
        }

        [Route("export")]
        [HttpGet]
        public string export(string param)
        {
            try
            {
                service.export(param);
            }
            catch (Exception)
            {

                return "导出失败";
            }
            return "已导出";
        }

        [Route("import")]
        [HttpGet]
        public string import(string param)
        {
            try
            {
                service.import(param);
            }
            catch (Exception)
            {

                return "导入失败";
            }
            return "已导入";
        }

    }
}
